package com.twotiger.and.activity.account.investtransfer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.alibaba.fastjson.JSON;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.account.invested.InverstedRecordPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.ProjectDetailrepayAdapter;
import com.twotiger.and.bean.DebtHasBuy;
import com.twotiger.and.bean.DebtTransed;
import com.twotiger.and.bean.DebtTransing;
import com.twotiger.and.bean.Investment;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.URLUtil;
import com.twotiger.and.util.ViewUtils;
import com.twotiger.and.view.UnConflictListView;

/**
 * 转让详情 -- 转让中、已完成、已购买 同一个页面按type区别展示
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月12日 下午2:50:02
 */
public class InverstedTransferDetailPage extends BaseActivity {

    private TextView inverst_time;
    private TextView projct_chinese_name;
    private TextView projct_cycle_time;
    private TextView Inverst_cash;
    private TextView receive_money_value;
    private TextView wait_recieve_text;
    private UnConflictListView invest_detail_list;
    private HashMap<String, String> parm;
    private SubTitle subTitle;
    private RelativeLayout mid_transing_layout;
    private LinearLayout mid_transed_layout;
    private RelativeLayout mid_hasbuy_layout;
    private View view_needgone;
    private LinearLayout bom_transingandtransed_layout, bom_hasbuy_layout;
    private TextView tag_trans, tag_discount;
    private TextView tv_project_investamount;
    private TextView tv_project_yearyield;
    private TextView tv_project_remiandays;
    private TextView tv_time_starttrans;
    private TextView tv_time_endtrans;
    private TextView tv_discount_rate;
    private TextView tv_realmoney;
    private TextView receive_money_text, wait_recieve_value, projct_cycle_text, invest_cash_text;
    private ProjectCommingJob projectCommingJob;
    private LinearLayout ll_above;
    private TextView tv_countdown;
    private TextView tv_unit_1;
    private TextView tv_unit_2;
    private TextView tv_unit_3;
    private TextView tv_unit_4;

    @Override
    protected void processLogic() {
        parm = getParm();
        initPage();
    }

    private void initPage() {
        String type = getIntent().getStringExtra("TYPE");
        if (InvestTransferPage.TRANSING.equals(type)) { //转让中
            ll_above.setVisibility(View.GONE);
            mid_transing_layout.setVisibility(View.VISIBLE);
            bom_transingandtransed_layout.setVisibility(View.VISIBLE);

            subTitle.setTitle("转让中项目");
            projct_cycle_text.setText("投资金额");
            invest_cash_text.setText("已转本金");
            receive_money_text.setText("打折率");
            wait_recieve_value.setText("到手金额");
            tv_unit_1.setText("元");
            tv_unit_2.setText("元");
            tv_unit_3.setText("%");
            tv_unit_4.setText("元");

            DebtTransing mDebtTransing = (DebtTransing) getIntent().getSerializableExtra("DETAIL");
            initDataOfDebtTransing(mDebtTransing);
        } else if (InvestTransferPage.TRANSED.equals(type)) { //已完成
            ll_above.setVisibility(View.GONE);
            mid_transed_layout.setVisibility(View.VISIBLE);
            bom_transingandtransed_layout.setVisibility(View.VISIBLE);

            subTitle.setTitle("已完成项目");
            projct_cycle_text.setText("投资金额");
            invest_cash_text.setText("已转本金");
            receive_money_text.setText("打折率");
            wait_recieve_value.setText("到手金额");
            tv_unit_1.setText("元");
            tv_unit_2.setText("元");
            tv_unit_3.setText("%");
            tv_unit_4.setText("元");

            DebtTransed mDebtTransed = (DebtTransed) getIntent().getSerializableExtra("DETAIL");
            initDataOfDebtTransed(mDebtTransed);
        } else if (InvestTransferPage.HASBUY.equals(type)) { //已购买
            ll_above.setVisibility(View.VISIBLE);
            mid_hasbuy_layout.setVisibility(View.VISIBLE);
            bom_hasbuy_layout.setVisibility(View.VISIBLE);
            tag_trans.setVisibility(View.GONE);

            subTitle.setTitle("已购买项目");
            projct_cycle_text.setText("项目周期");
            invest_cash_text.setText("投资金额");
            receive_money_text.setText("年利率");
            wait_recieve_value.setText("应收收益");
            tv_unit_1.setText("天");
            tv_unit_2.setText("元");
            tv_unit_3.setText("%");
            tv_unit_4.setText("元");

            DebtHasBuy mDebtHasBuy = (DebtHasBuy) getIntent().getSerializableExtra("DETAIL");
            initDataOfDebtHasBuy(mDebtHasBuy);
        }

    }

    /**
     * 填充已购买页面的数据
     * 
     * @param mDebtHasBuy
     */
    private void initDataOfDebtHasBuy(DebtHasBuy mDebtHasBuy) {

        //折扣比例为100%的时候，折扣信息的布局不显示
        if (100 == mDebtHasBuy.getAssignRate()) {
            mid_hasbuy_layout.setVisibility(View.GONE);
            view_needgone.setVisibility(View.VISIBLE);
        } else {
            mid_hasbuy_layout.setVisibility(View.VISIBLE);
            view_needgone.setVisibility(View.VISIBLE);
        }

        inverst_time.setText(DateUtil.getDateStringForLong(
            Long.valueOf(mDebtHasBuy.getInvestTime()), DateUtil.DATE_FMT));
        projct_chinese_name.setText(mDebtHasBuy.getName());
        if (mDebtHasBuy.getDiscountMark() == 0) {
            tag_discount.setVisibility(View.GONE);
        } else if (mDebtHasBuy.getDiscountMark() == 1) {
            tag_discount.setVisibility(View.GONE);
            tag_discount
                .setText(ArithUtils.coverMoney(mDebtHasBuy.getAssignRate() / 10 + "") + "折");
        }
        projct_cycle_time.setText(mDebtHasBuy.getPeriod() + "");
        Inverst_cash.setText(ArithUtils.coverMoneyComma(mDebtHasBuy.getInvestAmount() + ""));
        receive_money_value.setText(ArithUtils.coverMoneyComma(mDebtHasBuy.getYield() + "") + "");
        wait_recieve_text.setText(ArithUtils.coverMoneyComma(mDebtHasBuy.getYsInterest() + ""));
        tv_discount_rate.setText(ArithUtils.coverMoneyComma(mDebtHasBuy.getAssignRate() / 10 + "") + "");
        tv_realmoney.setText(ArithUtils.coverMoneyComma(mDebtHasBuy.getCostAmount() + ""));

        if (StringUtils.isEmpty(mDebtHasBuy.getpType())) {
            subTitle.title_right_tv.setVisibility(View.GONE);
        } else {
            subTitle.title_right_tv.setVisibility(View.VISIBLE);
        }

        List<Investment> list = JSON.parseArray(mDebtHasBuy.getPlanList(), Investment.class);
        if (!ListUtils.isEmpty(list)) {
            invest_detail_list.setAdapter(new ProjectDetailrepayAdapter(mContext, list,
                InverstedRecordPage.REQTYPE_CYZ));
        }
    }

    /**
     * 填充已完成页面的数据
     * 
     * @param mDebtTransed
     */
    private void initDataOfDebtTransed(DebtTransed mDebtTransed) {
        inverst_time.setText(mDebtTransed.getName());
        projct_chinese_name.setText(mDebtTransed.getName());
        if (mDebtTransed.getAssignMark() == 0) {
            tag_trans.setVisibility(View.GONE);
        } else if (mDebtTransed.getAssignMark() == 1) {
            tag_trans.setVisibility(View.GONE);
        }
        if (mDebtTransed.getDiscountMark() == 0) {
            tag_discount.setVisibility(View.GONE);
        } else if (mDebtTransed.getDiscountMark() == 1) {
            tag_discount.setVisibility(View.GONE);
            tag_discount.setText(ArithUtils.coverMoney(mDebtTransed.getAssignRate() / 10 + "")
                                 + "折");
        }
        projct_cycle_time.setText(ArithUtils.coverMoneyComma(mDebtTransed.getInvestAmount() + ""));
        Inverst_cash.setText(ArithUtils.coverMoneyComma(mDebtTransed.getCompletedVal() + ""));
        receive_money_value.setText(ArithUtils.coverMoneyComma(mDebtTransed.getAssignRate() + "")
                                    + "");
        wait_recieve_text.setText(ArithUtils.coverMoneyComma(mDebtTransed.getDealProceeds() + ""));
        tv_time_starttrans
            .setText(""
                     + DateUtil.getDateStringForLong(Long.valueOf(mDebtTransed.getStartTime()),
                         DateUtil.DATE_FMT));
        tv_time_endtrans.setText(""
                                 + DateUtil.getDateStringForLong(
                                     Long.valueOf(mDebtTransed.getDeadline()), DateUtil.DATE_FMT));
        tv_project_investamount.setText(""
                                        + ArithUtils.coverMoneyComma(mDebtTransed.getInvestAmount()
                                                                     + ""));
        tv_project_yearyield.setText("" + ArithUtils.coverMoneyComma(mDebtTransed.getYield() + ""));
        tv_project_remiandays.setText("" + mDebtTransed.getRemainDays() + "");
    }

    /**
     * 填充转让中页面的数据
     * 
     * @param mDebtTransing
     */
    private void initDataOfDebtTransing(DebtTransing mDebtTransing) {
        inverst_time.setText(mDebtTransing.getName());
        projct_chinese_name.setText(mDebtTransing.getName());
        if (mDebtTransing.getAssignMark() == 0) {
            tag_trans.setVisibility(View.GONE);
        } else if (mDebtTransing.getAssignMark() == 1) {
            tag_trans.setVisibility(View.GONE);
        }
        if (mDebtTransing.getDiscountMark() == 0) {
            tag_discount.setVisibility(View.GONE);
        } else if (mDebtTransing.getDiscountMark() == 1) {
            tag_discount.setVisibility(View.GONE);
            tag_discount.setText(ArithUtils.coverMoney(mDebtTransing.getAssignRate() / 10 + "")
                                 + "折");
        }
        projct_cycle_time.setText(ArithUtils.coverMoneyComma(mDebtTransing.getInvestAmount() + ""));
        Inverst_cash.setText(ArithUtils.coverMoneyComma(mDebtTransing.getCompletedVal() + ""));
        receive_money_value.setText(ArithUtils.coverMoneyComma(mDebtTransing.getAssignRate() + "")
                                    + "");
        wait_recieve_text.setText(ArithUtils.coverMoneyComma(mDebtTransing.getDealProceeds() + ""));
        tv_countdown.setText("" + mDebtTransing.getTime2Start());
        tv_project_investamount.setText(""
                                        + ArithUtils.coverMoneyComma(mDebtTransing
                                            .getInvestAmount() + ""));
        tv_project_yearyield
            .setText("" + ArithUtils.coverMoneyComma(mDebtTransing.getYield() + ""));
        tv_project_remiandays.setText("" + mDebtTransing.getRemainDays() + "");

        projectCommingJob = new ProjectCommingJob(mDebtTransing);
        projectCommingJob.start();
    }

    @Override
    protected void setListener() {

        subTitle.title_right_tv.setOnClickListener(this);
    }

    @Override
    protected void findViewById(View view) {

        mid_transing_layout = (RelativeLayout) view.findViewById(R.id.mid_transing_layout); //中间-转让中布局
        tv_countdown = (TextView) view.findViewById(R.id.tv_countdown);
        mid_transed_layout = (LinearLayout) view.findViewById(R.id.mid_transed_layout); //中间-已完成布局
        mid_hasbuy_layout = (RelativeLayout) view.findViewById(R.id.mid_hasbuy_layout);//中间-已购买布局
        view_needgone = view.findViewById(R.id.view_needgone);
        bom_transingandtransed_layout = (LinearLayout) view
            .findViewById(R.id.bom_transingandtransed_layout); //下部-转让中和已完成布局
        bom_hasbuy_layout = (LinearLayout) view.findViewById(R.id.bom_hasbuy_layout); //下部-已购买布局

        inverst_time = (TextView) view.findViewById(R.id.inverst_time);
        projct_chinese_name = (TextView) view.findViewById(R.id.projct_chinese_name);
        projct_cycle_time = (TextView) view.findViewById(R.id.projct_cycle_time);
        Inverst_cash = (TextView) view.findViewById(R.id.Inverst_cash);
        receive_money_value = (TextView) view.findViewById(R.id.receive_money_value);
        wait_recieve_text = (TextView) view.findViewById(R.id.wait_recieve_text);
        invest_detail_list = (UnConflictListView) view.findViewById(R.id.invest_detail_list);
        invest_detail_list.setFocusable(false);

        tag_trans = (TextView) view.findViewById(R.id.tag_trans);
        tag_discount = (TextView) view.findViewById(R.id.tag_discount);
        tv_project_investamount = (TextView) view.findViewById(R.id.tv_project_investamount);
        tv_project_yearyield = (TextView) view.findViewById(R.id.tv_project_yearyield);
        tv_project_remiandays = (TextView) view.findViewById(R.id.tv_project_remiandays);
        tv_time_starttrans = (TextView) view.findViewById(R.id.tv_time_starttrans);
        tv_time_endtrans = (TextView) view.findViewById(R.id.tv_time_endtrans);
        tv_discount_rate = (TextView) view.findViewById(R.id.tv_discount_rate);
        tv_realmoney = (TextView) view.findViewById(R.id.tv_realmoney);
        ll_above = (LinearLayout) view.findViewById(R.id.ll_above);

        projct_cycle_text = (TextView) view.findViewById(R.id.projct_cycle_text);
        invest_cash_text = (TextView) view.findViewById(R.id.invest_cash_text);
        receive_money_text = (TextView) view.findViewById(R.id.receive_money_text);
        wait_recieve_value = (TextView) view.findViewById(R.id.wait_recieve_value);

        //上部布局的单位
        tv_unit_1 = (TextView)view.findViewById(R.id.tv_unit_1);
        tv_unit_2 = (TextView)view.findViewById(R.id.tv_unit_2);
        tv_unit_3 = (TextView)view.findViewById(R.id.tv_unit_3);
        tv_unit_4 = (TextView)view.findViewById(R.id.tv_unit_4);

        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.hideBtns();
        subTitle.setTitle("项目详情");
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
        subTitle.title_right_tv.setText("协议");
        subTitle.title_right_tv.setVisibility(View.GONE);
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.inverst_transfer_detail_layout, null);
        return view;
    }

    /**
     * 获取token
     * @return
     */

    public String getToken() {
        return application.getUser().token;
    }

    @Override
    public void onClick(View view) {

        if (view == null) {
            return;
        }
        switch (view.getId()) {
        case R.id.sub_title_right_tv:

            if (View.VISIBLE != subTitle.title_right_tv.getVisibility()) {
                return;
            }
            DebtHasBuy mDebtHasBuy = (DebtHasBuy) getIntent().getSerializableExtra("DETAIL");
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (mDebtHasBuy == null) { //保证点击的时候invest不为空，要不报错
                return;
            }
            Intent in = new Intent(mContext, WebViewPage.class);
            in.putExtra("title", "合同");
            Map<String, String> contractmap = new HashMap<String, String>();
            contractmap.put("pType", mDebtHasBuy.getpType());
            contractmap.put("investId", mDebtHasBuy.getInvestId());
            contractmap.put("token", getToken());
            in.putExtra(
                "url",
                Constants.HOST + Constants.CONTRACT + "contractHtml?"
                        + URLUtil.createContractUrl(contractmap, "3.0"));
            startNewActivity(in, R.anim.push_left_in, R.anim.push_left_out, false);
            break;

        default:
            break;
        }
    }

    class ProjectCommingJob implements Runnable {

        /**每个县城只能start一次*/
        private boolean isRun = false;
        private boolean stop = false;

        private DebtTransing debtTransing;

        public ProjectCommingJob(DebtTransing mDebtTransing) {
            this.debtTransing = mDebtTransing;
        }

        public void stop() {
            this.stop = true;
        }

        public void start() {
            if (isRun)
                return;
            isRun = true;
            stop = false;
            handler.post(this);
        }

        @Override
        public void run() {
            if (!"已完成".equals(debtTransing.getTime2Start())) {
                tv_countdown.setText("" + debtTransing.getTime2Start());
            }
            if (stop) {
                return;
            }
            handler.postDelayed(this, 1000);
        }
    }

}
